package Threads.ReentrantLock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class trylockWithTimeoutTest {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            System.out.println("start");
            try {
                //这里应该是在1s之类，一直在尝试获取锁
                if (!lock.tryLock(1, TimeUnit.SECONDS)) {
                    System.out.println("fail!");
                    return;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                System.out.println("t1 get the lock");
            } finally {
                lock.unlock();
            }
        }, "t1");
        lock.lock();
        System.out.println("get the lock");
        t1.start();
        try {
            Thread.sleep(2000);
            System.out.println("Done!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
